<?php
/**
 * Created by PhpStorm.
 * User: tianrui
 * Date: 2018/1/29 0029
 * Time: 9:13
 */
class Department extends CI_Controller
{

    //主页
    public function index(){$this->load->view('department/allDepartment');}
    //添加部门
    public function addDepartment(){$this->load->view('department/addDepartment');}
    //修改部门
    public function updateDepartment(){$this->load->view('department/updateDepartment');}
    public $token_data;
    public function __construct(){
        parent::__construct();
        $this->load->model('department_model');
        $userToken = new Usertoken_helper();
        $this->token_data['token'] = $userToken->check_token($this);
        $this->token_data['user_token'] = $userToken->check_userToken($this);


    }
    /**
     * 添加接口
     */
    public function dep_add(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
            $data = $_POST['data'];
            $arr = json_decode($data, true);
            $this->department_model->addDepartment($arr);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }else {
            echo '0';
        }
    }

    /**
     * departmentList接口
     */
    public function dep_index(){
        //如果访问合法便进行数据操纵
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
            $data = $this->department_model->getAll();
            $this->token_data['data'] = $data;
        }
        $json = json_encode($this->token_data['data']);
        echo $json;
    }else {
            echo '0';
        }
    }

    /**
     * 删除接口
     */
    public function dep_del(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
            $id = $_POST['id'];
            $this->department_model->deleteDepartment($id);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }else {
            echo '0';
        }
    }

    /**
     * 修改接口
     */
    public function dep_update(){
        if($this->token_data['user_token']['flag']) {
            if ($this->token_data['token']['tag']) {
            $data = $_POST['data'];
            $arr = json_decode($data, true);
            $this->department_model->updateDepartment($arr);
            $this->token_data['success'] = 1;
        }
       $json = json_encode($this->token_data);

       echo $json;
    }else {
            echo '0';
        }
    }

    /**
     * 回显接口
     */
    public function dep_edit(){
            if($this->token_data['user_token']['flag']) {
                if ($this->token_data['token']['tag']) {
            $id = $_POST['id'];
            $this->token_data['department'] = $this->department_model->getDepartmentById($id);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }else {
                echo '0';
            }
    }

    /**
     * 模糊查询接口

    public function search(){
        if($this->token_data['tag']){
            $role_search = $_POST['role_search'];
            $this->token_data['data'] = $this->role_model->getSearch($role_search);
            $this->token_data['success'] = 1;
        }
        $json = json_encode($this->token_data);
        echo $json;
    }
     */
}